﻿var CereriConcediiFilter = function(container) {
    var self = this;
    var isInitialized = false;
    var isVisible = false;
    var lastTop = 0;

    var currentFilter = {
        tipConcediu: null,
        statusConcediu: null,
        punctDeLucru: null,
        departament: null,
        dataStart: null,
        dataSfarsit: null,
        marca: null,
        id: null
    };

    var isScrolledDisabled = false;

    this.hasAnyCriteriaSelected = function() {
        if (!currentFilter)
            return false;

        return currentFilter.tipConcediu || currentFilter.statusConcediu || currentFilter.punctDeLucru || currentFilter.departament || currentFilter.dataStart || currentFilter.dataSfarsit;
    };

    this.getFilterData = function() { return currentFilter; };

    var Elements = {
        tipConcediu: container.find('.tipConcediu .selectedValues'),
        statusConcediu: container.find('.statusConcediu .selectedValues'),
        punctDeLucru: container.find('.punctDeLucru .selectedValues'),
        departament: container.find('.departament .selectedValues'),
        dataStart: container.find('.dataStart .selectedValues'),
        dataSfarsit: container.find('.dataSfarsit .selectedValues'),
        dataStartInput: container.find('.dataStart .dataStartInput'),
        dataSfarsitInput: container.find('.dataSfarsit .dataSfarsitInput'),
        closeFilterButton: container.find(".closeFilterButton"),
        applyFilterButton: container.find('.applyFilterButton'),
        filterExplanation: null,
        overlay: container.parents('.container').find("#grayOverlay")
    };

    this.setFilterData = function(data) {
        self.setTipConcediu(data.tipConcediu);
        self.setStatusConcediu(data.statusConcediu);
        self.setPunctDeLucru(data.punctDeLucru);
        self.setDepartament(data.departament);
        self.setDataStart(data.dataStart);
        self.setDataSfarsit(data.dataSfarsit);
        currentFilter.ignoreSelf = data.ignoreSelf;
        currentFilter.marca = data.marca;
        currentFilter.id = data.id;

        self.updateFilterExplanation();
    };

    this.updateFilterExplanation = function() {
        return;
        if (!Elements.filterExplanation) {
            Elements.filterExplanation = app.CurrentPage.container.find('.filterExplanation');
            Elements.filterExplanation.tap(function() {
                self.toggle();
            });
        }

        var count = 0;
        if (currentFilter.tipConcediu)
            count++;
        if (currentFilter.statusConcediu)
            count++;
        if (currentFilter.punctDeLucru)
            count++;
        if (currentFilter.departament)
            count++;
        if (currentFilter.dataStart)
            count++;
        if (currentFilter.dataSfarsit)
            count++;

        if (!count)
            Elements.filterExplanation.hide();
        else {
            var text = count == 1 ? translate('Filter_Explanation_OneCriteria') : translate('Filter_Explanation_MoreCriteria').format(count);
            Elements.filterExplanation.show().find('span:first').html(text);
        }
    };

    var initialize = function() {
        /* START tip concediu */
        Elements.tipConcediu.click(function(e) {
            if (!app.Pages.SelectareTipConcediu.instance) {
                app.Pages.SelectareTipConcediu.instance = new SelectareTipConcediuPage(app.Pages.SelectareTipConcediu.container);
            }
            app.Pages.SelectareTipConcediu.instance.setCallback(function(data) {
                self.setTipConcediu(data);
                app.changePage(app.Pages.CereriConcedii);
                self.show();
            });
            app.changePage(app.Pages.SelectareTipConcediu);
            return Event.stopEvent(e);
        });

        Elements.tipConcediu.parent().find('.cancel').click(function() { resetTipConcediu($(this)); });;
        /* END tip concediu */

        /* START status concediu */
        Elements.statusConcediu.click(function(e) {
            if (!app.Pages.SelectareStatusConcediu.instance) {
                app.Pages.SelectareStatusConcediu.instance = new SelectareStatusConcediuPage(app.Pages.SelectareStatusConcediu.container);
                app.Pages.SelectareStatusConcediu.instance.initialize();
            }
            app.changePage(app.Pages.SelectareStatusConcediu);
            app.Pages.SelectareStatusConcediu.instance.refresh(function(data) {
                self.setStatusConcediu(data);
                app.changePage(app.Pages.CereriConcedii);
            });
            return Event.stopEvent(e);
        });

        Elements.statusConcediu.parent().find('.cancel').click(function() { resetStatusConcediu($(this)); });
        /* END status concediu */

        /* START punct de lucru */
        Elements.punctDeLucru.click(function(e) {
            if (!app.Pages.SelectarePunctDeLucru.instance) {
                app.Pages.SelectarePunctDeLucru.instance = new SelectarePunctDeLucruPage(app.Pages.SelectarePunctDeLucru.container);
                app.Pages.SelectarePunctDeLucru.instance.initialize();
            }
            app.changePage(app.Pages.SelectarePunctDeLucru);
            app.Pages.SelectarePunctDeLucru.instance.setCallback(function(data) {
                self.setPunctDeLucru(data);
                app.changePage(app.Pages.CereriConcedii);
                self.show();
            });
            return Event.stopEvent(e);
        });

        Elements.punctDeLucru.parent().find('.cancel').click(function() { resetPunctDeLucru($(this)); });
        /* END punct de lucru */

        /* START departament */
        Elements.departament.parent().find('.cancel').click(function() { resetDepartament($(this)); });
        /* END departament */

        /* START data start */
        Utils.initializeDatePicker(Elements.dataStartInput, function(valueText, inst) {
            var dataStart = Elements.dataStartInput.scroller('getDate');
            self.setDataStart(dataStart);
            return false;
        });

        Elements.dataStart.click(function(e) {
            Elements.dataStartInput.scroller('show');
            return Event.stopEvent(e);
        });
        Elements.dataStart.parents('.dataStart').find('.cancel').click(function() { self.setDataStart(null); });
        /* END data start */

        /* START data finala */
        Utils.initializeDatePicker(Elements.dataSfarsitInput, function(valueText, inst) {
            var dataSfarsit = Elements.dataSfarsitInput.scroller('getDate');
            self.setDataSfarsit(dataSfarsit);
            return false;
        });

        Elements.dataSfarsit.click(function(e) {
            Elements.dataSfarsitInput.scroller('show');
            return Event.stopEvent(e);
        });
        Elements.dataSfarsit.parents('.dataSfarsit').find('.cancel').click(function() { self.setDataSfarsit(null); });
        /* END data finala */

        Elements.closeFilterButton.click(function(e) {
            self.hide();
            return Event.stopEvent(e);
        });

        Elements.applyFilterButton.click(function(e) {
            applyFilter();
            return Event.stopEvent(e);
        });

        $(document).bind('touchmove', function(e) {
            if (app.CurrentPage.instance == app.Pages.CereriConcedii.instance && isScrolledDisabled)
                return Event.stopEvent(e);
        });

        $(document).bind('touchstart', function() {
            if (app.CurrentPage.instance == app.Pages.CereriConcedii.instance && self.isVisible()) {
                if ($(document).scrollTop() != lastTop)
                    $(document).scrollTop(lastTop);
            }
        });

        if (!Elements.overlay.length) {
            Elements.overlay = app.CurrentPage.container.find("#grayOverlay");
            Elements.overlay.bind('touchstart', function(e) { return Event.stopEvent(e); });
        }

        isInitialized = true;
    };

    var applyFilter = function() {
        self.updateFilterExplanation();
        container.trigger('apply');
        self.hide();
    };

    var departamentSelected = function() {
        if (!currentFilter.punctDeLucru || !currentFilter.punctDeLucru.DepartmentsCount)
            return;

        if (!app.Pages.SelectareDepartament.instance) {
            app.Pages.SelectareDepartament.instance = new SelectareDepartamentPage(app.Pages.SelectareDepartament.container);
            app.Pages.SelectareDepartament.instance.initialize();
        }
        app.changePage(app.Pages.SelectareDepartament);
        app.Pages.SelectareDepartament.instance.refresh(currentFilter.punctDeLucru, function(data) {
            self.setDepartament(data);
            app.changePage(app.Pages.CereriConcedii);
            self.show();
        });
    };

    var resetTipConcediu = function(object) {
        currentFilter.tipConcediu = null;
        Elements.tipConcediu.html(translate('General_Toate'));
        if (object)
            object.hide();
        self.show();
    };

    var resetStatusConcediu = function(object) {
        currentFilter.statusConcediu = null;
        Elements.statusConcediu.html(translate('General_Toate'));
        if (object)
            object.hide();
    };

    var resetPunctDeLucru = function(object) {
        self.setPunctDeLucru(null);
        if (object)
            object.hide();
        self.show();
    };

    var resetDepartament = function(object) {
        self.setDepartament(null);
        if (object)
            object.hide();
        self.show();
    };

    this.hasAnyCriteriaSelected = function() {
        return currentFilter.tipConcediu || currentFilter.statusConcediu || currentFilter.punctDeLucru || currentFilter.departament || currentFilter.dataStart || currentFilter.dataSfarsit;
    };

    this.reset = function() {
        self.setFilterData({});
    };

    this.setTipConcediu = function(data) {
        currentFilter.tipConcediu = data;
        if (data) {
            if (data.Subtip) {
                Elements.tipConcediu.html(data.Subtip.STP_NUME);
            } else Elements.tipConcediu.html(data.Tip.Nume);
            Elements.tipConcediu.parent().find('.cancel').show();
        } else {
            Elements.tipConcediu.html(translate('General_Toate'));
            Elements.tipConcediu.parent().find('.cancel').hide();
        }
    };

    this.setStatusConcediu = function(data) {
        currentFilter.statusConcediu = data;
        Elements.statusConcediu.html(data ? data.Nume : translate('General_Toate'));
        if (data)
            Elements.statusConcediu.parent().find('.cancel').show();
        else Elements.statusConcediu.parent().find('.cancel').hide();
    };

    this.setPunctDeLucru = function(pct) {
        if (!currentFilter.punctDeLucru || !pct || currentFilter.punctDeLucru.Id != pct.Id) {
            self.setDepartament(null);
        }

        if (!pct || (pct && pct.DepartmentsCount == 0)) {
            Elements.departament.addClass('disabled');
            Elements.departament.unbind();
        } else {
            Elements.departament.unbind().click(function(e) {
                departamentSelected();
                return Event.stopEvent(e);
            });
            Elements.departament.removeClass('disabled');
        }

        currentFilter.punctDeLucru = pct;
        if (pct) {
            Elements.punctDeLucru.html(pct.Name);
            Elements.punctDeLucru.parent().find('.cancel').show();
        } else {
            Elements.punctDeLucru.html(translate('General_Toate'));
            Elements.punctDeLucru.parent().find('.cancel').hide();
        }
    };

    this.setDepartament = function(dpt) {
        currentFilter.departament = dpt;
        if (dpt) {
            Elements.departament.html(dpt.Name);
            Elements.departament.parent().find('.cancel').show();
        } else {
            Elements.departament.html(translate('General_Toate'));
            Elements.departament.parent().find('.cancel').hide();
        }
    };

    this.setDataStart = function(dataStart) {
        currentFilter.dataStart = dataStart;
        if (dataStart) {
            Elements.dataStart.html(Globalize.format(dataStart, "d").toString());
            if (currentFilter.dataSfarsit != null && currentFilter.dataSfarsit < dataStart)
                self.setDataSfarsit(dataStart);
            Elements.dataStart.parents('.dataStart').find('.cancel').show();
        } else {
            Elements.dataStart.parents('.dataStart').find('.cancel').hide();
            Elements.dataStart.html(translate('General_Oricand'));
        }
    };

    this.setDataSfarsit = function(dataSfarsit) {
        currentFilter.dataSfarsit = dataSfarsit;
        if (dataSfarsit) {
            Elements.dataSfarsit.html(Globalize.format(dataSfarsit, "d").toString());
            if (currentFilter.dataStart != null && currentFilter.dataStart > dataSfarsit)
                self.setDataStart(dataSfarsit);
            Elements.dataSfarsit.parents('.dataSfarsit').find('.cancel').show();
        } else {
            Elements.dataSfarsit.parents('.dataSfarsit').find('.cancel').hide();
            Elements.dataSfarsit.html(translate('General_Oricand'));
        }
    };

    this.resetMarca = function() {
        currentFilter.marca = null;
    };

    this.resetId = function() {
        currentFilter.id = null;
    };

    this.show = function() {
        if (!isInitialized)
            initialize();

        if (!isVisible)
            lastTop = $(document).scrollTop();
        container.css('top', lastTop + 'px').show();
        Elements.overlay.show().css({
            top: (container.position().top + container.outerHeight()) + 'px'
        });

        isScrolledDisabled = container.position().top + container.outerHeight() - $(document).scrollTop() < $(window).height();

        $(document).trigger('overlayOpened');
        isVisible = true;
    };

    this.hide = function() {
        if (container.is(":visible")) {
            container.hide();
            Elements.overlay.hide();
            isScrolledDisabled = false;
            $(document).trigger('overlayClosed');
            lastTop = 0;
            isVisible = false;
        }
    };

    this.toggle = function() {
        if (container.is(":visible"))
            self.hide();
        else self.show();
    };

    this.isVisible = function() {
        return isVisible;
    };

    this.isAnyDialogVisible = function() {
        return Elements.dataStartInput.attr("visible") == 'visible' || Elements.dataSfarsitInput.attr("visible") == 'visible';
    };

    this.closeDialogs = function() {
        if (Elements.dataStartInput.attr("visible") == 'visible')
            Elements.dataStartInput.scroller('hide');
        if (Elements.dataSfarsitInput.attr("visible") == 'visible')
            Elements.dataSfarsitInput.scroller('hide');
    };
};